import 'package:flutter/material.dart';
import 'package:flutter_hplus/theme/theme.dart';

/// 表格分页组件
class TablePagination extends StatelessWidget {
  final int currentPage;
  final int totalPages;
  final int rowsPerPage;
  final int totalItems;
  final List<int> rowsPerPageOptions;
  final Function(int) onPageChanged;
  final Function(int) onRowsPerPageChanged;

  const TablePagination({
    super.key,
    required this.currentPage,
    required this.totalPages,
    required this.rowsPerPage,
    required this.totalItems,
    required this.rowsPerPageOptions,
    required this.onPageChanged,
    required this.onRowsPerPageChanged,
  });

  @override
  Widget build(BuildContext context) {
    final theme = HTheme.of(context);
    
    return Padding(
      padding: const EdgeInsets.only(top: 16.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          // 显示当前页信息
          Text(
            '显示 ${(currentPage - 1) * rowsPerPage + 1} 到 ${currentPage * rowsPerPage > totalItems ? totalItems : currentPage * rowsPerPage} 条，共 $totalItems 条',
            style: theme.textStyles.bodySmall,
          ),
          const SizedBox(width: 16),
          // 每页行数选择
          Row(
            children: [
              Text('每页显示', style: theme.textStyles.body),
              const SizedBox(width: 8),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8.0),
                decoration: BoxDecoration(
                  border: Border.all(color: theme.colors.divider),
                  borderRadius: BorderRadius.circular(theme.dimens.borderRadius),
                  color: theme.colors.surface,
                ),
                child: DropdownButton<int>(
                  value: rowsPerPage,
                  focusColor: theme.colors.surface,
                  dropdownColor: theme.colors.surface,
                  items: rowsPerPageOptions.map((int value) {
                    return DropdownMenuItem<int>(
                      value: value,
                      child: Text('$value'),
                    );
                  }).toList(),
                  onChanged: (value) {
                    if (value != null) {
                      onRowsPerPageChanged(value);
                    }
                  },
                  underline: Container(), // 移除下划线
                  icon: Icon(Icons.arrow_drop_down, color: theme.colors.text),
                  isDense: true, // 使下拉框更紧凑
                ),
              ),
              Text(' 条记录', style: theme.textStyles.body),
            ],
          ),
          
          const Spacer(),
          
          // 上一页按钮
          IconButton(
            icon: const Icon(Icons.chevron_left),
            onPressed: currentPage > 1 
                ? () => onPageChanged(currentPage - 1) 
                : null,
            color: currentPage > 1 ? theme.colors.primary : theme.colors.textDisabled,
          ),
          
          // 页码按钮
          ..._buildPageButtons(theme),
          
          // 下一页按钮
          IconButton(
            icon: const Icon(Icons.chevron_right),
            onPressed: currentPage < totalPages 
                ? () => onPageChanged(currentPage + 1) 
                : null,
            color: currentPage < totalPages ? theme.colors.primary : theme.colors.textDisabled,
          ),
        ],
      ),
    );
  }
  
  /// 构建页码按钮
  List<Widget> _buildPageButtons(HTheme theme) {
    const maxVisiblePages = 5;
    final List<Widget> pageButtons = [];
    
    // 计算显示的页码范围
    int startPage = currentPage - maxVisiblePages ~/ 2;
    int endPage = currentPage + maxVisiblePages ~/ 2;
    
    // 调整范围确保不超出总页数
    if (startPage < 1) {
      startPage = 1;
      endPage = startPage + maxVisiblePages - 1;
      if (endPage > totalPages) {
        endPage = totalPages;
      }
    }
    
    if (endPage > totalPages) {
      endPage = totalPages;
      startPage = endPage - maxVisiblePages + 1;
      if (startPage < 1) {
        startPage = 1;
      }
    }
    
    // 添加第一页按钮
    if (startPage > 1) {
      pageButtons.add(
        _buildPageButton(1, theme),
      );
      
      // 添加省略号
      if (startPage > 2) {
        pageButtons.add(
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 8.0),
            child: Text('...'),
          ),
        );
      }
    }
    
    // 添加中间页码按钮
    for (int i = startPage; i <= endPage; i++) {
      pageButtons.add(
        _buildPageButton(i, theme),
      );
    }
    
    // 添加最后一页按钮
    if (endPage < totalPages) {
      // 添加省略号
      if (endPage < totalPages - 1) {
        pageButtons.add(
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 8.0),
            child: Text('...'),
          ),
        );
      }
      
      pageButtons.add(
        _buildPageButton(totalPages, theme),
      );
    }
    
    return pageButtons;
  }
  
  /// 构建单个页码按钮
  Widget _buildPageButton(int page, HTheme theme) {
    final isCurrentPage = page == currentPage;
    
    return InkWell(
      onTap: isCurrentPage ? null : () => onPageChanged(page),
      borderRadius: BorderRadius.circular(4),
      child: Container(
        width: 32,
        height: 32,
        margin: const EdgeInsets.symmetric(horizontal: 4),
        decoration: BoxDecoration(
          color: isCurrentPage ? theme.colors.primary : Colors.transparent,
          borderRadius: BorderRadius.circular(4),
          border: Border.all(
            color: isCurrentPage ? theme.colors.primary : theme.colors.divider,
          ),
        ),
        child: Center(
          child: Text(
            '$page',
            style: theme.textStyles.body.copyWith(
              color: isCurrentPage ? Colors.white : theme.colors.text,
              fontWeight: isCurrentPage ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ),
      ),
    );
  }
} 